package cn.micro.system.manage.service.impl;

import cn.micro.system.manage.common.enums.BusinessTypeEnum;
import cn.micro.system.manage.entity.AssetDimProductVersion;
import cn.micro.system.manage.entity.AssetOdsFileRecordUseRelation;
import cn.micro.system.manage.mapper.AssetDimProductVersionMapper;
import cn.micro.system.manage.service.AssetDimProductVersionService;
import cn.micro.system.manage.service.AssetOdsFileRecordUseRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品版本信息表 服务实现类
 * </p>
 *
 * @author wr
 * @since 2024-01-05
 */
@Service
@RequiredArgsConstructor
public class AssetDimProductVersionServiceImpl extends ServiceImpl<AssetDimProductVersionMapper, AssetDimProductVersion> implements AssetDimProductVersionService {

    private final AssetOdsFileRecordUseRelationService assetOdsFileRecordUseRelationService;
    @Override
    @Transactional
    public boolean pub(AssetDimProductVersion productVersion) {
        boolean result = this.baseMapper.insert(productVersion) >0;
        if (productVersion.getFileIds() != null && productVersion.getFileIds().size() > 0){
            List<AssetOdsFileRecordUseRelation> useRelationList = new ArrayList<>();
            for (Long fileId : productVersion.getFileIds()){
                AssetOdsFileRecordUseRelation useRelation = new AssetOdsFileRecordUseRelation();
                useRelation.setUseId(String.valueOf(productVersion.getId()));
                useRelation.setFileId(fileId);
                useRelation.setBusinessType(BusinessTypeEnum.PRODUCT_BUSINESS.getValue());  //产品信息
                useRelationList.add(useRelation);
            }
            return assetOdsFileRecordUseRelationService.saveBatch(useRelationList);
        }
        return result;
    }
}
